import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:aichat_flutter_wallet/module/module_dapp/provider/dapp_cache_provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

class DAppCacheUtil {
  static deleteHistoryOne(
    BuildContext context, {
    required DAppBean dApp,
  }) async {
    try {
      bool isExisted =
          await Provider.of<DAppCacheProvider>(context, listen: false)
              .isCacheDAppExisted(dApp);
      if (isExisted) {
        await Provider.of<DAppCacheProvider>(context, listen: false)
            .removeCache(dApp);
      }
    } catch (e) {
      kaiPrint('catch :${e}');
    }
  }

  static deleteAllHistory(
    BuildContext context, {
    required List<DAppBean> dApps,
  }) async {
    for (int i = dApps.length - 1; i >= 0; i--) {
      var dApp = dApps[i];
      await deleteHistoryOne(context, dApp: dApp);
    }
  }
}
